/*
* Copyright© 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.netty.server.server.module.lbs.passenger;

import com.google.protobuf.InvalidProtocolBufferException;
import com.laihuichuxing.netty.common.constant.TopicConstant;
import com.laihuichuxing.netty.common.core.session.Session;
import com.laihuichuxing.netty.common.core.session.SessionManager;
import com.laihuichuxing.netty.common.mq.dto.PassengerPositionDto;
import com.laihuichuxing.netty.common.mq.producer.PositionProducer;
import com.laihuichuxing.netty.server.server.module.Common;
import com.laihuichuxing.netty.server.server.module.login.entity.Login;
import com.laihuichuxing.protobuf.module.LbsModule;
import com.laihuichuxing.protobuf.module.OrderModule;
import com.laihuichuxing.socket.LbsCmd;
import com.laihuichuxing.socket.ModuleId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * Description: 乘客定位handler
 *
 * @author YangGuang
 * @since: 1.0
 * Create Date Time: 2018/10/10 16:53
 * Update Date Time:
 */
@Component
public class PassengerPositionHandlerImpl implements PassengerPositionHandler {

    private Logger logger = LoggerFactory.getLogger(PassengerPositionHandlerImpl.class);

    @Autowired
    private PositionProducer positionProducer;

    @Autowired
    private TopicConstant topicConstant;

    @Override
    public void passengerOrder(Session session, byte[] data) {
        try {
            // 反序列化,获取客户端发来的数据
            LbsModule.PassengerPosition passengerPosition = LbsModule.PassengerPosition.parseFrom(data);
            // 获取当前用户会话
            Object object = session.getAttachment();
            if (object != null) {
                Login login = (Login) object;
                // 如果不是null,放入mq
                putPositionMQ(login, passengerPosition, topicConstant.getPassengerOrder());
                // 给客户端回消息
                Common.lbsNotifyClient(session,LbsCmd.PASSENGER_ORDER);
            }
        } catch (InvalidProtocolBufferException e) {
            logger.error("乘客下单定位protocol异常:{}", e.toString(), e);
        }  catch (Exception e) {
            logger.error("乘客下单定位异常:{}", e.toString(), e);
        }
    }

    @Override
    public void passengerCancelOrder(Session session, byte[] data) {
        try {
            OrderModule.Order order = OrderModule.Order.parseFrom(data);
            SessionManager.sendMessage(order.getDriverMobile(), ModuleId.LBS, LbsCmd.PASSENGER_CANCEL, order);
            // 给客户端回消息
            Common.lbsNotifyClient(session,LbsCmd.PASSENGER_CANCEL);
        } catch (InvalidProtocolBufferException e) {
            logger.error("乘客取消订单反序列化异常:{}", e.toString(), e);
        }
    }

    @Override
    public void driverDeparture(Session session, byte[] data) {
        try {
            // 反序列化数据
            OrderModule.Order order = OrderModule.Order.parseFrom(data);
            // 给客户端发消息
            SessionManager.sendMessage(order.getPassengerMobile(), ModuleId.LBS, LbsCmd.DRIVER_DEPARTURE, order);
            // 给客户端回消息
            Common.lbsNotifyClient(session,LbsCmd.DRIVER_DEPARTURE);
        } catch (InvalidProtocolBufferException e) {
            logger.error("司机发车通知乘客反序列化异常:{}", e.toString(), e);
        }
    }

    @Override
    public void driverReceipt(Session session, byte[] data) {
        try {
            // 反序列化数据
            OrderModule.Order order = OrderModule.Order.parseFrom(data);
            // 给客户端发消息
            SessionManager.sendMessage(order.getPassengerMobile(), ModuleId.LBS, LbsCmd.DRIVER_RECEIPT, order);
            // 给客户端回消息
            Common.lbsNotifyClient(session,LbsCmd.DRIVER_RECEIPT);
        } catch (InvalidProtocolBufferException e) {
            logger.error("司机确认到达通知乘客支付反序列化异常:{}", e.toString(), e);
        }
    }

    @Override
    public void returnPosition(Session session, byte[] data) {
        try {
            // 反序列化数据
            LbsModule.ReturnPosition returnPosition = LbsModule.ReturnPosition.parseFrom(data);
            // 给客户端发消息
            SessionManager.sendMessage(returnPosition.getMobile(), ModuleId.LBS, LbsCmd.RETURN_POSITION, returnPosition);
        } catch (InvalidProtocolBufferException e) {
            logger.error("互传位置反序列化异常:{}", e.toString(), e);
        }
    }

    @Override
    public void takeNotify(Session session, byte[] data) {
        try {
            // 反序列化数据
            OrderModule.Order order = OrderModule.Order.parseFrom(data);
            // 给客户端发消息
            SessionManager.sendMessage(order.getPassengerMobile(), ModuleId.LBS, LbsCmd.DRIVER_TAKE, order);
        } catch (InvalidProtocolBufferException e) {
            logger.error("上车通知乘客反序列化异常:{}", e.toString(), e);
        }
    }

    /**
     * Description: 乘客下单位置mq
     *
     * @param login             login
     * @param passengerPosition passengerPosition
     * @return:
     * @author YangGuang
     * Create Date Time: 2018/10/10 17:16
     * Update Date Time:
     */
    private void putPositionMQ(Login login, LbsModule.PassengerPosition passengerPosition, String topic)  {
        PassengerPositionDto passengerPositionDto = new PassengerPositionDto();
        passengerPositionDto.setLongitude(passengerPosition.getLongitude());
        passengerPositionDto.setLatitude(passengerPosition.getLatitude());
        passengerPositionDto.setCityCode(passengerPosition.getCityCode());
        passengerPositionDto.setMobile(login.getMobile());
        positionProducer.position(passengerPositionDto, topic);
    }
}
